<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a {
            display: block;
            text-align: center;
            text-decoration: none;
            color: #000;
        }

        .dialog {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 400px;
            height: 200px;
            background: #fff;
            box-shadow: 0 0 15px rgba(255, 255, 255, 0.5);
        }

        .box {
            cursor: move;
            height: 50px;
            text-align: center;
            line-height: 50px;
        }

        form {
            font-size: 14px;
            margin-top: 10px;
            padding-left: 5px;
        }

        span {
            display: inline-block;
            width: 80px;
            text-align: right;
        }

        .box1 {
            margin-bottom: 10px;
        }

        .x {
            width: 240px;
            height: 22px;
            margin-left: 10px;
        }

        .y {
            margin-top: 20px;
            margin-left: 50%;
            transform: translateX(-50%);
            height: 50px;
            width: 150px;
            height: 25px;
            text-align: center;
            background: #fff;
            border: 1px solid gray;

        }

        .shut {
            position: absolute;
            top: -10px;
            right: -10px;
            font-size: 10px;
            width: 30px;
            height: 30px;
            border-radius: 30px;
            background: #fff;
            line-height: 30px;
            text-align: center;
            font-family: "微软雅黑";
        }

        .bg {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: gray;
            z-index: -1;
        }
    </style>
</head>

<body>
    <a href="javascript:;" class="login" id="login1">点击，弹出登录框</a>
    <div class="dialog" id="dialog">
        <div class="box" id="login">登录会员</div>
        <form action="#" method="POST">

            <div class="box1"><span>用户名 :</span><input type="text" placeholder="请输入用户名" class="x"></div>
            <div class="box2"><span>登录密码 :</span><input type="password" placeholder="请输入登录密码" class="x"></div>
            <input type="submit" value="登录会员" class="y">
        </form>
        <div class="shut" id="shut">关闭</div>
    </div>
    <div class="bg" id="bg"></div>
    <script>
        var login = document.getElementById('login');
        var dialog = document.getElementById('dialog');
        var login1 = document.getElementById('login1');
        var bg = document.getElementById('bg');
        var shut = document.getElementById('shut');
        login1.addEventListener('click', function () {
            dialog.style.display = 'block';
            bg.style.display = 'block';
        })
        shut.addEventListener('click', function () {
            dialog.style.display = 'none';
            bg.style.display = 'none';
        })
        login.addEventListener('mousedown', function (e) {
            console.log('鼠标是' + e.pageX);
            console.log('盒子是' + dialog.offsetLeft);
            var x = e.pageX - dialog.offsetLeft;
            var y = e.pageY - dialog.offsetTop;
            document.addEventListener('mousemove', move);
            function move(e) {
                dialog.style.left = (e.pageX - x) + 'px';
                dialog.style.top = (e.pageY - y) + 'px';
            }
            document.addEventListener('mouseup', function (e) {
                document.removeEventListener('mousemove', move);
            })
        })
    </script>
</body>

</html>